package com.xique.door.service.impl;


import com.xique.door.util.RedisUtil;
import com.xique.door.bean.response.IotApiResponse;
import com.xique.door.constant.ElevatorConstant;
import com.xique.door.util.RestTemplateUtils;
import com.xique.door.service.IIotApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shd
 * @create 2021/10/9 16:28
 */
@Service
public class IotApiServiceImpl implements IIotApiService {

    @Value("${IOT_URL}")
    private String IOT_URL;

    @Value("${IOT_SYS_CODE}")
    private String IOT_SYS_CODE;

    @Value("${IOT_PASSWORD}")
    private String IOT_PASSWORD;

    private String url = "/zaagtech-1.0.0/iotinterface/";

    @Autowired
    private RestTemplateUtils restTemplateUtils;

    @Autowired
    private RedisUtil redisUtil;

    private Map<String, Object> getBaseParams() {
        Map<String, Object> map = new HashMap<>(3);
        map.put("sys_code", IOT_SYS_CODE);
        map.put("session_token", redisUtil.get(ElevatorConstant.IotSessionTokenRedisKey));
        return map;
    }

    @Override
    public IotApiResponse getMd5Token() {
        Map<String, Object> map = new HashMap<>(2);
        map.put("sys_code", IOT_SYS_CODE);
        map.put("password", IOT_PASSWORD);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.GET_MD5_TOKEN, map, null, IotApiResponse.class);
    }

    @Override
    public IotApiResponse getAccessToken(String token) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("sys_code", IOT_SYS_CODE);
        map.put("token", token);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.GET_ACCESS_TOKEN, map, null, IotApiResponse.class);
    }

    @Override
    public IotApiResponse getSessionToken(String accessToken) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("access_token", accessToken);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.GET_SESSION_TOKEN, map, null, IotApiResponse.class);
}

    @Override
    public IotApiResponse getElvByCompany() {
        Map<String, Object> map = getBaseParams();
        Map<String, Object> param = new HashMap<>(1);
        param.put("code", IOT_SYS_CODE);
        map.put("param", param);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.GET_ELV_COMPANY, map, null, IotApiResponse.class);
    }

    @Override
    public IotApiResponse getElvInfo(String imei) {
        Map<String, Object> map = getBaseParams();
        Map<String, Object> param = new HashMap<>(1);
        param.put("imei", imei);
        map.put("param", param);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.GET_ELV_INFO, map, null, IotApiResponse.class);
    }

    @Override
    public IotApiResponse getElvReport(String imei) {
        Map<String, Object> map = getBaseParams();
        Map<String, Object> param = new HashMap<>(1);
        param.put("imei", imei);
        map.put("param", param);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.GET_ELV_REPORT, map, null, IotApiResponse.class);
    }

    @Override
    public IotApiResponse getRunTime(String imei, String recordDate, Integer pageNumber, Integer pageSize) {
        Map<String, Object> map = getBaseParams();
        Map<String, Object> param = new HashMap<>(4);
        param.put("imei", imei);
        param.put("record_date", recordDate);
        param.put("page_number", pageNumber);
        param.put("page_size", pageSize);
        map.put("param", param);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.GET_RUN_TIME, map, null, IotApiResponse.class);
    }

    @Override
    public IotApiResponse getFault(String imei, String recordDate, Integer pageNumber, Integer pageSize) {
        Map<String, Object> map = getBaseParams();
        Map<String, Object> param = new HashMap<>(4);
        param.put("imei", imei);
        param.put("record_date", recordDate);
        param.put("page_number", pageNumber);
        param.put("page_size", pageSize);
        map.put("param", param);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.GET_FAULT, map, null, IotApiResponse.class);
    }

    @Override
    public IotApiResponse getEvent(String imei, String recordDate, Integer pageNumber, Integer pageSize) {
        Map<String, Object> map = getBaseParams();
        Map<String, Object> param = new HashMap<>(4);
        param.put("imei", imei);
        param.put("record_date", recordDate);
        param.put("page_number", pageNumber);
        param.put("page_size", pageSize);
        map.put("param", param);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.GET_EVENT, map, null, IotApiResponse.class);
    }

    @Override
    public IotApiResponse getHisRuntime(String imei, String startDate, String endDate, Integer pageNumber, Integer pageSize) {
        Map<String, Object> map = getBaseParams();
        Map<String, Object> param = new HashMap<>(5);
        param.put("imei", imei);
        param.put("start_date", startDate);
        param.put("end_date", endDate);
        param.put("page_number", pageNumber);
        param.put("page_size", pageSize);
        map.put("param", param);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.GET_HIS_RUN_TIME, map, null, IotApiResponse.class);
    }

    @Override
    public IotApiResponse getHisFault(String imei, String startDate, String endDate, Integer pageNumber, Integer pageSize) {
        Map<String, Object> map = getBaseParams();
        Map<String, Object> param = new HashMap<>(5);
        param.put("imei", imei);
        param.put("start_date", startDate);
        param.put("end_date", endDate);
        param.put("page_number", pageNumber);
        param.put("page_size", pageSize);
        map.put("param", param);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.GET_HIS_FAULT, map, null, IotApiResponse.class);
    }

    @Override
    public IotApiResponse getHisEvent(String imei, String startDate, String endDate, Integer pageNumber, Integer pageSize) {
        Map<String, Object> map = getBaseParams();
        Map<String, Object> param = new HashMap<>(5);
        param.put("imei", imei);
        param.put("start_date", startDate);
        param.put("end_date", endDate);
        param.put("page_number", pageNumber);
        param.put("page_size", pageSize);
        map.put("param", param);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.GET_HIS_EVENT, map, null, IotApiResponse.class);
    }

    @Override
    public IotApiResponse getFaultList(String imei, String startDate, String endDate) {
        Map<String, Object> map = getBaseParams();
        Map<String, Object> param = new HashMap<>(3);
        param.put("imei", imei);
        param.put("start_date", startDate);
        param.put("end_date", endDate);
        map.put("param", param);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.GET_FAULT_LIST, map, null, IotApiResponse.class);
    }

    @Override
    public IotApiResponse getElevatorParameter(String imei) {
        Map<String, Object> map = getBaseParams();
        Map<String, Object> param = new HashMap<>(1);
        param.put("imei", imei);
        map.put("param", param);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.GET_ELV_PARAM, map, null, IotApiResponse.class);
    }

    @Override
    public IotApiResponse getDeviceRunInfo(String imei) {
        Map<String, Object> map = getBaseParams();
        Map<String, Object> param = new HashMap<>(1);
        param.put("imei", imei);
        map.put("param", param);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.GET_RUN_INFO, map, null, IotApiResponse.class);
    }

    @Override
    public IotApiResponse setWarningPost(String notifyUrl) {
        Map<String, Object> map = getBaseParams();
        Map<String, Object> param = new HashMap<>(2);
        param.put("url", notifyUrl);
        param.put("pass_code", IOT_PASSWORD);
        map.put("param", param);
        return restTemplateUtils.httpPostJson(IOT_URL + url + ElevatorConstant.InterfaceName.SET_WARNING_POST, map, null, IotApiResponse.class);
    }
}
